package com.tuanzi.loan.schedule.receipt;

import com.base.common.http.HttpClientUtil;
import com.base.common.http.model.HttpResult;
import com.base.common.http.model.RequestHeader;
import com.base.common.log.LogService;
import com.tuanzi.loan.business.entity.statistics.LoanAdsChannelDetail;
import com.tuanzi.loan.business.service.statistics.LoanAdsChannelDetailService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.GsonUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Simon
 * @date 2019/07/29 14:05
 */
@Debug
@Slf4j
@Service
@Transactional
public class StatisticsAdvertiseTokenScheduler {

    @Autowired
    private LoanAdsChannelDetailService loanAdsChannelDetailService;

    @Value("${advertise.wechat.token.url}")
    private String wechatUrl;
    @Value("${advertise.guangdiantong.token.url}")
    private String guangDianTongUrl;

    private final static Integer WECHAT_ADS_PLATFORM_TYPE = 6;
    private final static Integer GUANGDIANTONG_ADS_PLATFORM_TYPE = 2;
    private final static Integer TECENTMONITOR_ADS_PLATFORM_TYPE = 12;

    @Scheduled(cron = "${cron.statistics-adver-wechat-refreshtoken-task}")
    public Boolean statisticWeChatToken(){
        LogService.info("微信广告账户配置token刷新");
        List<LoanAdsChannelDetail> details = loanAdsChannelDetailService.findAll(WECHAT_ADS_PLATFORM_TYPE);
        details.forEach(detail ->
                {
                    try {
                        HttpResult httpResult = HttpClientUtil.get(new RequestHeader(), String.format(wechatUrl, detail.getAppId(), detail.getAppSecret()));
                        String response = httpResult.getStringData();
                        log.info("微信广告token响应channel,{},response{}",detail.getChannelCode(),response);
                        WeChatTokenResponse weChatTokenResponse = GsonUtils.toObject(response, WeChatTokenResponse.class);
                        if(weChatTokenResponse.getAccess_token()!=null){
                            loanAdsChannelDetailService.updateAccessToken(detail.getChannelCode(),weChatTokenResponse.getAccess_token());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

        );
        return true;
    }

    @Scheduled(cron = "${cron.statistics-adver-qq-refreshtoken-task}")
    public Boolean statisticGuangDianTongToken(){
        LogService.info("广点通广告账户配置token刷新");
        List<LoanAdsChannelDetail> details = loanAdsChannelDetailService.findAll(GUANGDIANTONG_ADS_PLATFORM_TYPE);
        List<LoanAdsChannelDetail> tecentDetails = loanAdsChannelDetailService.findAll(TECENTMONITOR_ADS_PLATFORM_TYPE);
        details.addAll(tecentDetails);
        details.forEach(detail ->
                {
                    try {
                        HttpResult httpResult = HttpClientUtil.get(new RequestHeader(), String.format(guangDianTongUrl, detail.getAppId(), detail.getAppSecret(),detail.getRefreshToken()));
                        String response = httpResult.getStringData();
                        GuangDianTongTokenResponse guangDianTongTokenResponse = GsonUtils.toObject(response, GuangDianTongTokenResponse.class);
                        log.info("广点通token响应channel,{},response{}",detail.getChannelCode(),response);
                        GuangDianTongTokenData data = guangDianTongTokenResponse.getData();
                        if(data.getAccess_token()!=null){
                            loanAdsChannelDetailService.updateAccessToken(detail.getChannelCode(),data.getAccess_token());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

         );
        return true;
    }

    @Data
    public static class WeChatTokenResponse{

        private String access_token;

        private Integer expires_in;
    }

    @Data
    private static class GuangDianTongTokenResponse{
        private Integer code;
        private String message;
        private GuangDianTongTokenData data;
    }
    @Data
    private static class GuangDianTongTokenData {
        private String refresh_token;
        private String access_token;
        private Long access_token_expires_in;
    }
}
